﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VPSoftTestApplication.DAL;
using VPSoftTestApplication.Entity;
using VPSoftTestApplication.View;

namespace VPSoftTestApplication.Presenters
{
    public class RecordViewPresenter
    {
        private Inventory _inventory;
        private IRecordView _view;

        public RecordViewPresenter(Inventory inventory, IRecordView view)
        {
            _inventory = inventory;
            _view = view;
            _view.Barcode = _inventory.Barcode;
            _view.InputType = inventory.InputType;
            _view.Quantity = inventory.QuantityEdit;
            _view.Product = _inventory.Product;
            _view.Save += Save;
        }

        public void Save (object sender, EventArgs e)
        {
            _inventory.Barcode = _view.Barcode;
            _inventory.InputType = _view.InputType;
            _inventory.QuantityEdit = _view.Quantity;
            _inventory.Product = _view.Product;
            _inventory.QuantityDifference = _inventory.QuantityOriginal - _inventory.QuantityEdit;
            if (_inventory.IID == Guid.Empty)
            {
                _inventory.IID = Guid.NewGuid();
                DatabaseFactory.GetInstance().InventoryControler.Insert(_inventory);
            }

            else
            {
                DatabaseFactory.GetInstance().InventoryControler.Update(_inventory);
            }
        }
    }
}
